package com.tangding.service.dao.games.data;

import java.io.Serializable;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.bson.Document;
import org.bson.conversions.Bson;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;

import com.mongodb.client.AggregateIterable;
import com.mongodb.client.model.Accumulators;
import com.mongodb.client.model.Aggregates;
import com.mongodb.client.model.BsonField;
import com.mongodb.client.model.Filters;
import com.tangding.common.exception.GException;
import com.tangding.common.utils.DateUtils;
import com.tangding.service.db.BaseMongoRepository;
import com.tangding.service.db.MongoDBOption;
import com.tangding.service.db.MongoDBUtil;

/**
 * @description 
 *
 * @author Ranger
 * @version 2018年1月16日_下午4:45:16
 *
 */
//@Repository
public class SendRepository extends BaseMongoRepository<Document, Serializable>{
	
	@Value("${game_mongo_host:127.0.0.1:27017}")
    protected String hosts;
    
    @Value("${game_mongo_auth:}")
    protected String auths;

	protected void beforeInitClient() {
		super.hosts = this.hosts;
		super.auths = this.auths;
		
		option = new MongoDBOption("PlayCity", "UserPresentScore");
	}

	@Override
	protected void afterInitClient() {
		findTest();
	}
	
	public void findTest() {
		List<Bson> params = Arrays.asList(
			Aggregates.match(Filters.and(Filters.in("Sender", 100001, 100002), Filters.gte("Time", 1516120397), Filters.lte("Time", 1516125138))),
			Aggregates.group("$Sender", Accumulators.sum("TotalAmount", "$Amount"), Accumulators.first("Sender", "$Sender"))
		);
		
		AggregateIterable<Document> findAll = MongoDBUtil.aggregate(client, option, params);
		for (Document document : findAll) {
			System.out.println(document);
		}
	}
	
	public static void main(String[] args) {
		long hours8 = 8 * 3600 * 1000L;
		System.out.println(DateUtils.formatDate(new Date(1516125138 * 1000L - hours8), "yyyy-MM-dd HH:mm:ss"));
	}
}
